package com.ts.controller.achievement;

import com.ts.constant.AchievementConstant;
import com.ts.dto.PageDTO;
import com.ts.dto.PaperDTO;
import com.ts.entity.Paper;
import com.ts.entity.TsAchievement;
import com.ts.result.PageResult;
import com.ts.result.Result;
import com.ts.service.achievement.PaperService;
import com.ts.service.achievement.TsAchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 论文相关接口 - 简化版
 */
@RestController
@RequestMapping("/achievement/paper")
@Tag(name = "论文相关接口", description = "论文的增删改查及分页查询")
@Slf4j
public class PaperController {

    @Autowired
    private PaperService paperService;
    @Autowired
    private TsAchievementService tsAchievementService;

    /**
     * 新增论文（同步创建关联的成果记录）
     */
    @Operation(summary = "新增论文", description = "提交论文基本信息并同步创建成果记录")
    @PostMapping
    public Result save(
            @Parameter(description = "论文信息", required = true)
            @RequestBody PaperDTO paperDTO
    ) {
        log.info("新增论文：{}", paperDTO);


        Long paperId = paperDTO.getId();
        log.info("论文新增成功，ID：{}", paperId);

        // 2. 构建成果所需参数（从DTO中提取或构建）
        String achievementName = paperDTO.getPaperName(); // 成果名称复用论文名称
        // 构建成果描述（使用论文特有字段）
        String type = AchievementConstant.PAPER;
        String description = String.format("论文《%s》，发表于《%s》，作者：%s",
                paperDTO.getPaperName(),
                paperDTO.getPublishedJournal(),
                paperDTO.getAuthor());
       // Integer level = paperDTO.getAchievementSourceLevel(); // 假设DTO中有级别字段，或根据业务默认赋值
        Integer level = 1;// 假设DTO中有级别字段，或根据业务默认赋值
        Integer createdBy =  1;
        // 3. 同步新增关联的成果记录（论文类型）
        TsAchievement achievement = tsAchievementService.addAchievement(
                achievementName,
                type,
                description,
                level,
                createdBy,
                paperId
        );
        log.info("同步创建成果成功，成果ID：{}", achievement.getId());

        return Result.success();

    }

    /**
     * 根据ID删除论文
     */
    @Operation(summary = "删除论文", description = "根据ID删除指定论文")
    @DeleteMapping("/{id}")
    public Result delete(
            @Parameter(description = "论文ID", required = true)
            @PathVariable Long id
    ) {
        log.info("删除论文，ID：{}", id);
        paperService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改论文
     */
    @Operation(summary = "修改论文", description = "更新论文信息（需包含ID）")
    @PutMapping
    public Result update(
            @Parameter(description = "论文信息（含ID）", required = true)
            @RequestBody PaperDTO paperDTO
    ) {
        log.info("修改论文：{}", paperDTO);
        paperService.updateById(paperDTO);
        return Result.success();
    }

    /**
     * 根据ID查询论文
     */
    @Operation(summary = "查询单篇论文", description = "根据ID获取论文详情")
    @GetMapping("/{id}")
    public Result<Paper> getById(
            @Parameter(description = "论文ID", required = true)
            @PathVariable Long id
    ) {
        log.info("查询论文，ID：{}", id);
        Paper paper = paperService.getById(id);
        return Result.success(paper);
    }

    /**
     * 分页查询论文
     */
    @Operation(summary = "分页查询论文", description = "按页码和每页条数查询论文列表")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(
            @Parameter(description = "分页参数（页码、每页条数）")
                    PageDTO dto
    ) {
        log.info("论文分页查询，参数：{}", dto);
        PageResult pageResult = paperService.pageQuery(dto);
        return Result.success(pageResult);
    }
}